<?php
/**
 * Created by PhpStorm.
 * User: songdewei
 * Date: 2017/10/17
 * Time: 上午11:24
 */

namespace Model\Member;


use Data\Company\CompanyCatlogModel;
use Data\Company\CompanyContentModel;
use Data\Company\CompanyModel;

class CompanyController extends BaseController
{
    /**
     * CompanyController constructor.
     */
    function __construct()
    {
        parent::__construct();
        $this->var['_menu'] = 'company';
    }

    /**
     *
     */
    public function index(){
        global $_G,$_lang;

        $companyModel = new CompanyModel();
        if ($this->checkFormSubmit()) {
            $company = $_GET['company'];
            if ($company['company_name'] && $company['company_logo'] && $company['company_image']) {
                $company_id = intval($_GET['company_id']);
                if ($_GET['company_id']) {
                    $company['update_time'] = time();
                    $companyModel->where(array('uid'=>$this->uid, 'company_id'=>$company_id))->data($company)->save();
                    (new CompanyContentModel())->where(array('company_id'=>$company_id))->data(array('content'=>$_GET['content']))->save();
                }else {
                    $company['create_time'] = time();
                    $company_id = $companyModel->data($company)->add();
                    (new CompanyContentModel())->data(array(
                        'uid'=>$this->uid,
                        'company_id'=>$company_id,
                        'content'=>$_GET['content']
                    ))->add();
                }
                $this->showSuccess('save_succeed');
            }else {
                $this->showError('invalid_parameter');
            }
        }else {

            $company = $companyModel->where(array('uid'=>$this->uid))->getOne();
            $content = (new CompanyContentModel())->where(array('company_id'=>$company['company_id']))->getOne();
            $editorname = 'content';
            $editorcontent = $content['content'];

            $catloglist = (new CompanyCatlogModel())->getCatlogTree();

            $this->var['title'] = '资料设置';
            include template('company');
        }
    }
}